//
//  ExpressHabitViewController.m
//  app
//
//  Created by 王少宁 on 15/5/8.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNExpressHabitViewController.h"
#import "HZRemoteResult.h"
#import "HZMallCartsHttpManager.h"
#import "NSDictionary+SafetyGet.h"

@interface SNExpressHabitViewController ()
@property (nonatomic, retain) UIView * payHabitView;
@property (nonatomic, retain) UIView * expressHabitView;

@property (strong, nonatomic) HZRemoteResult *dataResult;
@end

@implementation SNExpressHabitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [XCommon hexStringToColor:kColor_m_light_gray_background];
    self.navigationItem.title = @"支付配送";
    
    self.dataResult = [HZRemoteResult target:self action:@selector(finishLoadData)];
    
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self createPayHabitView];
    [self createExpressHabitView];
}

#pragma mark 完成加载数据
- (void)finishLoadData{
    if (self.dataResult.response.status == JKResponseOK) {
        [self createPayHabitView];
        [self createExpressHabitView];
    }
}
- (void)createPayHabitView
{
    NSDictionary *pay= self.pay;
    
    NSArray * btnTitleArray = [pay arrayForKey:@"options"];
    
    NSInteger payHabitViewHeight = 60 + 60*((btnTitleArray.count+1)/2);
    
    _payHabitView = [[UIView alloc] initWithFrame:CGRectMake(0, self.navigationAndStatusBarHeight+20, SCREEN_WIDTH, payHabitViewHeight)];
    _payHabitView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_payHabitView];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH-20, 20)];
    
    label.text = [pay stringForKey:@"title" nilValue:@"支付方式"];
    [label setFont:JKAppGlobleFontMenu];
    [_payHabitView addSubview:label];
    
    CGFloat width = (SCREEN_WIDTH - 20*3)*0.5;
    CGFloat height = 40;
    
    
    for (int i = 0; i < btnTitleArray.count; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = [btnTitleArray[i] integerForKey:@"payType"]+100;

        btn.frame = CGRectMake(20*(i+1)+i*width, label.ufo.yy+20, width, height);
        btn.enabled = !self.payHabitBtnNotEnabled;
        [btn setTitle:[btnTitleArray[i] stringForKey:@"title" nilValue:@"支付"] forState:UIControlStateNormal];
        btn.titleLabel.font = JKAppGlobleFontSmallTagText;
        btn.layer.borderWidth = UP(1);
        [btn addTarget:self action:@selector(payHabitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_payHabitView addSubview:btn];
        
        if ([btnTitleArray[i] boolForKey:APIKEY_mall_selected]) {
            self.payHibitType = [btnTitleArray[i] stringForKey:APIKEY_mall_title nilValue:@""];
            self.payHibitNum = [btnTitleArray[i] stringForKey:APIKEY_mall_payType nilValue:@"0"];
            [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            btn.layer.borderColor = [UIColor orangeColor].CGColor;
        }
        
    }
    
    [self payHabitBtnColro];
}

- (void)payHabitBtnColro
{
    
    for (UIView * view in self.payHabitView.subviews) {
        if ([view.class isSubclassOfClass:[UIButton class]]) {
            UIButton * button = (UIButton*)view;
            if ([button.currentTitle isEqualToString:self.payHibitType]) {
                [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
                button.layer.borderColor = [UIColor orangeColor].CGColor;
            }else{
                [button setTitleColor:[XCommon hexStringToColor:kColor_m_light_gray_text] forState:UIControlStateNormal];
                button.layer.borderColor = [XCommon hexStringToColor:kColor_m_light_gray_text].CGColor;
            }

        }
    }
    
}

- (void)payHabitBtnClick:(UIButton *)btn
{
    self.payHibitType = btn.currentTitle;
    self.payHibitNum = [NSString stringWithFormat:@"%d", btn.tag - 100];
    
    for (UIView * view in self.payHabitView.subviews) {
        if ([view.class isSubclassOfClass:[UIButton class]]) {
            UIButton * button = (UIButton*)view;
            if (button.tag == btn.tag) {
                [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
                button.layer.borderColor = [UIColor orangeColor].CGColor;
            } else {
                [button setTitleColor:[XCommon hexStringToColor:kColor_m_light_gray_text] forState:UIControlStateNormal];
                button.layer.borderColor = [XCommon hexStringToColor:kColor_m_light_gray_text].CGColor;
            }
        }
    }
}

- (void)createExpressHabitView
{
    NSDictionary *delivery= self.delivery;
    NSArray * btnTitleArray = [delivery arrayForKey:@"options"];
    
    NSInteger row = btnTitleArray.count;
    if (row > 1) {
        row--;
    }
    
    _expressHabitView = [[UIView alloc] initWithFrame:CGRectMake(0, _payHabitView.ufo.yy+20, SCREEN_WIDTH, row*60 + 60)];
    _expressHabitView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_expressHabitView];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH-20, 20)];
    
    label.text = [delivery stringForKey:@"title" nilValue:@"配送方式"];
    [label setFont:JKAppGlobleFontMenu];
    [_expressHabitView addSubview:label];
    
    CGFloat width = (SCREEN_WIDTH - 20*3)*0.5;
    CGFloat height = 40;
    
    for (int i = 0; i < btnTitleArray.count; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i+1000;
        if (i > 1) {
            btn.frame = CGRectMake(20, label.ufo.yy+(height+20)*(i-1)+20, SCREEN_WIDTH-40, height);
        }else{
            btn.frame = CGRectMake(20*(i+1)+i*width, label.ufo.yy+20, width, height);
        }
        
        btn.enabled = !self.expressHabitBtnNotEnabled;
        [btn setTitle:[btnTitleArray[i] stringForKey:@"title" nilValue:@"配送"] forState:UIControlStateNormal];
        btn.titleLabel.font = JKAppGlobleFontSmallTagText;
        btn.layer.borderWidth = UP(1);
        [btn addTarget:self action:@selector(expressHabitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_expressHabitView addSubview:btn];
        
        if ([btnTitleArray[i] boolForKey:APIKEY_mall_selected]) {
            self.dispatchType = [btnTitleArray[i] stringForKey:APIKEY_mall_title nilValue:@""];
            [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            btn.layer.borderColor = [UIColor orangeColor].CGColor;
        }
    }
    
    [self expressHabitBtnColor];
}

- (void)expressHabitBtnColor
{
    for (UIView * view in self.expressHabitView.subviews) {
        if ([view.class isSubclassOfClass:[UIButton class]]) {
            UIButton * button = (UIButton*)view;
            
            if ([self.dispatchType isEqualToString:button.currentTitle]) {
                [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
                button.layer.borderColor = [UIColor orangeColor].CGColor;
            
            } else {
                [button setTitleColor:[XCommon hexStringToColor:kColor_m_light_gray_text] forState:UIControlStateNormal];
                button.layer.borderColor = [XCommon hexStringToColor:kColor_m_light_gray_text].CGColor;
            }
        }
    }

}

- (void)expressHabitBtnClick:(UIButton *)btn
{
    self.dispatchType = btn.currentTitle;
    
    for (UIView * view in self.expressHabitView.subviews) {
        if ([view.class isSubclassOfClass:[UIButton class]]) {
            UIButton * button = (UIButton*)view;
            if (button.tag == btn.tag) {
                [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
                button.layer.borderColor = [UIColor orangeColor].CGColor;
            } else {
                [button setTitleColor:[XCommon hexStringToColor:kColor_m_light_gray_text] forState:UIControlStateNormal];
                button.layer.borderColor = [XCommon hexStringToColor:kColor_m_light_gray_text].CGColor;
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if ([self.deletage respondsToSelector:@selector(clickBtnWithPayHabit:expressHabit:)]) {
        
        NSMutableDictionary *pay = [NSMutableDictionary dictionary];
        NSMutableArray *payArray = [NSMutableArray array];
        [pay setObject:[self.pay stringForKey:APIKEY_mall_title nilValue:@""] forKey:APIKEY_mall_title];
        
        for (NSDictionary *dict in [self.pay arrayForKey:APIKEY_mall_options]) {
            NSMutableDictionary *tDict = [NSMutableDictionary dictionaryWithDictionary:dict];
            if ([[dict stringForKey:APIKEY_mall_payType nilValue:@""] isEqualToString:self.payHibitNum]) {
                [tDict setValue:@"1" forKey:APIKEY_mall_selected];
            }else{
                [tDict setValue:@"0" forKey:APIKEY_mall_selected];
            }
            [payArray addObject:tDict];
        }
        
        [pay setObject:payArray forKey:APIKEY_mall_options];
        
        self.pay = pay;
        
        NSMutableDictionary *delivery = [NSMutableDictionary dictionary];
        NSMutableArray *deliveryArray = [NSMutableArray array];
        
        for (NSMutableDictionary *dict in [self.delivery arrayForKey:APIKEY_mall_options]) {
            NSMutableDictionary *tDict = [NSMutableDictionary dictionaryWithDictionary:dict];
            if ([[dict stringForKey:APIKEY_mall_title nilValue:@""] isEqualToString:self.dispatchType]) {
                [tDict setValue:@"1" forKey:APIKEY_mall_selected];
            }else{
                [tDict setValue:@"0" forKey:APIKEY_mall_selected];
            }
            [deliveryArray addObject:tDict];
        }
        [delivery setObject:deliveryArray forKey:APIKEY_mall_options];
        
        self.delivery = delivery;
        
        [self.deletage performSelector:@selector(clickBtnWithPayHabit:expressHabit:) withObject:self.pay withObject:self.delivery];
    }
}



@end
